package com.songlanyun.modules.shop.controller.admin;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.entity.UserShopRoleEntity;
import com.songlanyun.modules.exception.MemberException;
import com.songlanyun.modules.shop.model.dto.ShopUserRoleConfigDTO;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.shop.entity.ShopUserRoleConfigEntity;
import com.songlanyun.modules.shop.service.ShopUserRoleConfigService;
import com.songlanyun.common.utils.R;


/**
 * 商家的会员等级配置，（本系统，不提供商家创建自己的等级，只能系统自动给商家初始化）
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-05 18:07:51
 */
@RestController
@RequestMapping("admin/shop/shopUserRoleConfig")
public class ShopUserRoleConfigController extends AbstractController {
    @Autowired
    private ShopUserRoleConfigService shopUserRoleConfigService;

    @Autowired
    private ShopService shopService;


    /**
     * 商家的会员等级配置列表
     */
    @GetMapping("/select")
//    @RequiresPermissions("shop:shopUserRoleConfig:list")
    public R select() {
        Long shopId = getShopId();
        if (shopId == 0) {//平台使用平台商家的
            shopId = shopService.selectPlatformShopId(false);
            if (ObjectUtil.isNull(shopId)) {
                return R.ok();
            }
        }
        List<ShopUserRoleConfigEntity> list = shopUserRoleConfigService.select(shopId);
        return R.ok().put("list", list);
    }

    /**
     * 商家的会员等级配置详情
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("shop:shopUserRoleConfig:list")
    public R info(@PathVariable("id") Long id) {
        ShopUserRoleConfigEntity data = shopUserRoleConfigService.getById(id);
        return R.ok().put("data", data);
    }


    /**
     * 修改
     */
    @SysLog("修改商家的会员等级配置")
    @PutMapping("/update")
//    @RequiresPermissions("shop:shopUserRoleConfig:update")
    public R update(@RequestBody ShopUserRoleConfigDTO dto) {
        ValidatorUtils.validateEntity(dto);

        ShopUserRoleConfigEntity obj = shopUserRoleConfigService.update(getShopId(), dto);
        return R.ok().put("data", obj);
    }

    /**
     * 获取指定店铺的等级设置
     */
    @GetMapping("/getShopConfig")
    public R getConfigByShop() {
        Long shopId = getShopId();
        if (ObjectUtil.isNull(shopId) || shopId == 0L) {
            throw new RRException(MemberException.STORES_CAN_ONLY_MODIFY_MEMBERSHIP_LEVELS);
        }
        List<ShopUserRoleConfigEntity> list = shopUserRoleConfigService.list(
                new LambdaQueryWrapper<ShopUserRoleConfigEntity>()
                        .eq(ShopUserRoleConfigEntity::getShopId, shopId)
                        .ne(ShopUserRoleConfigEntity::getLevel, MemberConstant.MemberLevel.CITY_AGENT.getCode())//排除市代
                        .orderByAsc(ShopUserRoleConfigEntity::getLevel)
        );

        return R.ok().put("data", list);
    }


}
